package org.dromara.system.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.common.excel.core.ExcelResult;
import org.dromara.system.domain.vo.WbCandidateListVo;
import org.dromara.system.domain.vo.WbMinuteBidBagHistoryVo;
import org.dromara.system.listener.WbCandidateInfoHistoryImportListener;
import org.dromara.system.listener.WbCandidateInfoListener;
import org.dromara.system.listener.WbCandidateListListener;
import org.dromara.system.listener.WbMinuteBidBagHistoryImportListener;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.system.domain.vo.WbCandidateInfoVo;
import org.dromara.system.domain.bo.WbCandidateInfoBo;
import org.dromara.system.service.IWbCandidateInfoService;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 推荐中标候选人公示
 *
 * @author Lion Li
 * @date 2024-05-27
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/candidateInfo")
public class WbCandidateInfoController extends BaseController {

    private final IWbCandidateInfoService wbCandidateInfoService;
    /**
     * 发布
     *
     * @param ids 主键串
     */
    @Log(title = "发布中标候选人公示")
    @GetMapping("/issue/{ids}")
    public R<Void> issueCandidate(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(wbCandidateInfoService.issueCandidate(List.of(ids)));
    }

    /**
     * 撤回
     *
     * @param ids 主键串
     */
    @Log(title = "发布中标候选人公示")
    @GetMapping("/withdraw/{ids}")
    public R<Void> withdraw(@NotEmpty(message = "主键不能为空")
                                  @PathVariable Long[] ids) {
        return toAjax(wbCandidateInfoService.withdraw(List.of(ids)));
    }

    /**
     * 查询推荐中标候选人公示列表
     */
    @SaCheckPermission("system:candidateInfo:list")
    @GetMapping("/list")
    public TableDataInfo<WbCandidateInfoVo> list(WbCandidateInfoBo bo, PageQuery pageQuery) {
        return wbCandidateInfoService.queryPageList(bo, pageQuery);
    }

    /**
     * 导入历史数据数据
     * @param file          导入文件
     */
    @Log(title = "候选人结果公告", businessType = BusinessType.IMPORT)
    @SaCheckPermission("system:candidateInfo:add")
    @PostMapping(value = "/importDataHistory", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<Void> importDataHistory(@RequestPart("file") MultipartFile file) throws Exception {
        ExcelResult<WbMinuteBidBagHistoryVo> result = ExcelUtil.importExcel(file.getInputStream(), WbMinuteBidBagHistoryVo.class,new WbCandidateInfoHistoryImportListener());
        return R.ok(result.getAnalysis());
    }

    /**
     * 下载历史模板
     * @param response
     */
    @SaCheckPermission("system:candidateInfo:export")
    @Log(title = "候选人结果公告", businessType = BusinessType.EXPORT)
    @PostMapping("/exportTemplateHistory")
    public void exportTemplateHistory( HttpServletResponse response) {
        WbMinuteBidBagHistoryVo wbMinuteBidBagHistoryVo = new WbMinuteBidBagHistoryVo();
        List<WbMinuteBidBagHistoryVo> list = new ArrayList<>();
        wbMinuteBidBagHistoryVo.setItemName("XX项目名称");
        wbMinuteBidBagHistoryVo.setSubStandardName("XX产品名称/分标名称");
        wbMinuteBidBagHistoryVo.setYear("2024");
        wbMinuteBidBagHistoryVo.setPrice("1.16（注意单位万元）");
        wbMinuteBidBagHistoryVo.setProvince("江西省");
        wbMinuteBidBagHistoryVo.setBagNo("包X");
        wbMinuteBidBagHistoryVo.setCompanyName("XX公司");
        list.add(wbMinuteBidBagHistoryVo);
        ExcelUtil.exportExcel(list, "推荐候选人公告", WbMinuteBidBagHistoryVo.class, response);
    }

    /**
     * 导出推荐中标候选人公示列表
     */
    @SaCheckPermission("system:candidateInfo:export")
    @Log(title = "推荐中标候选人公示", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(WbCandidateInfoBo bo, HttpServletResponse response) {
        List<WbCandidateInfoVo> list = wbCandidateInfoService.queryList(bo);
        ExcelUtil.exportExcel(list, "推荐中标候选人公示", WbCandidateInfoVo.class, response);
    }

    /**
     * 获取推荐中标候选人公示详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("system:candidateInfo:query")
    @GetMapping("/{id}")
    public R<WbCandidateInfoVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(wbCandidateInfoService.queryById(id));
    }

    /**
     * 新增推荐中标候选人公示
     */
    @SaCheckPermission("system:candidateInfo:add")
    @Log(title = "推荐中标候选人公示", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody WbCandidateInfoBo bo) {
        return toAjax(wbCandidateInfoService.insertByBo(bo));
    }

    /**
     * 修改推荐中标候选人公示
     */
    @SaCheckPermission("system:candidateInfo:edit")
    @Log(title = "推荐中标候选人公示", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody WbCandidateInfoBo bo) {
        return toAjax(wbCandidateInfoService.updateByBo(bo));
    }

    /**
     * 删除推荐中标候选人公示
     *
     * @param ids 主键串
     */
    @SaCheckPermission("system:candidateInfo:remove")
    @Log(title = "推荐中标候选人公示", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(wbCandidateInfoService.deleteWithValidByIds(List.of(ids), true));
    }

    /**
     * 导出推荐候选人列表模板
     */
    @SaCheckPermission("system:candidateInfo:export")
    @Log(title = "推荐候选人", businessType = BusinessType.EXPORT)
    @PostMapping("/exportTemplate")
    public void exportTemplate(HttpServletResponse response) {
        List<WbCandidateInfoVo> list = new ArrayList<>();
        WbCandidateInfoVo wbCandidateInfoVo = new WbCandidateInfoVo();
        wbCandidateInfoVo.setTenderee("招标人");
        wbCandidateInfoVo.setTenderAgent("招标代理机构");
        wbCandidateInfoVo.setHeadline("公示标题");
        wbCandidateInfoVo.setPurchaseNo("采购编号");
        wbCandidateInfoVo.setPublicityTime(new Date());
        list.add(wbCandidateInfoVo);
        ExcelUtil.exportExcel(list, "推荐候选人", WbCandidateInfoVo.class, response);
    }

    /**
     * 导入数据
     *
     * @param file          导入文件
     */
    @Log(title = "推荐候选人", businessType = BusinessType.IMPORT)
    @SaCheckPermission("system:candidateInfo:add")
    @PostMapping(value = "/importData", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<Void> importData(@RequestPart("file") MultipartFile file) throws Exception {
        ExcelResult<WbCandidateInfoVo> result = ExcelUtil.importExcel(file.getInputStream(), WbCandidateInfoVo.class,new WbCandidateInfoListener());
        return R.ok(result.getAnalysis());
    }

    public void updateStatus(Long candidateId, String status) {
        wbCandidateInfoService.updateStatus(candidateId, status);
    }

}
